package vip.mengqin.diandezhun.ui.activity;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.baselibrary.interface_.OkHttpCallBack;
import com.android.baselibrary.responce.BaseResponce;
import com.android.baselibrary.tool.CommLoading;
import com.android.baselibrary.tool.CommToast;
import com.android.baselibrary.view.Comm_HeadView;
import com.android.baselibrary.view.MultiStateView;
import com.xgr.alipay.alipay.AliPay;
import com.xgr.alipay.alipay.AlipayInfoImpli;
import com.xgr.easypay.EasyPay;
import com.xgr.easypay.callback.IPayCallback;

import butterknife.BindView;
import butterknife.OnClick;
import vip.mengqin.diandezhun.R;
import vip.mengqin.diandezhun.manager.API_Manager;
import vip.mengqin.diandezhun.responce.PayInfo_AliPayData;

public class PayActivity extends BaseActivity {

    @BindView(R.id.multiplestatusView)
    MultiStateView multiplestatusView;
    @BindView(R.id.comm_title)
    Comm_HeadView comm_title;
    @BindView(R.id.iv_weixin)
    ImageView iv_weixin;
    @BindView(R.id.iv_zhifubao)
    ImageView iv_zhifubao;
    @BindView(R.id.ll_weixin)
    LinearLayout ll_weixin;
    @BindView(R.id.ll_zhifubao)
    LinearLayout ll_zhifubao;
    @BindView(R.id.tv_price)
    TextView tv_price;
    @BindView(R.id.tv_ordername)
    TextView tv_ordername;

    int orderId;
    double price;
    String orderName;

    String payment;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_pay;
    }

    @Override
    protected void initView() {
        comm_title.setTitle("支付订单");

        orderId = getIntent().getIntExtra("orderId", 0);
        price = getIntent().getDoubleExtra("price", 0);
        orderName = getIntent().getStringExtra("orderName");

        tv_price.setText("￥ " + price + "");
        tv_ordername.setText(orderName);
    }

    @Override
    protected void initData() {
        ll_weixin.performClick();
    }

    @OnClick({R.id.ll_weixin, R.id.ll_zhifubao, R.id.csb_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_weixin:
                payment = "wxpay";
                checkPayMode();
                break;
            case R.id.ll_zhifubao:
                payment = "alipay";
                checkPayMode();
                break;
            case R.id.csb_ok:
                order_payinfo();
                break;
        }
    }

    private void checkPayMode() {
        if ("wxpay".equals(payment)) {
            iv_weixin.setImageResource(R.drawable.ico_select);
            iv_zhifubao.setImageResource(R.drawable.ico_unselect);
            ll_weixin.setBackgroundResource(R.drawable.shape_circle_stroke_ff8b00);
            ll_zhifubao.setBackgroundResource(R.drawable.shape_circle_stroke_dddddd);
        } else {
            iv_weixin.setImageResource(R.drawable.ico_unselect);
            iv_zhifubao.setImageResource(R.drawable.ico_select);
            ll_weixin.setBackgroundResource(R.drawable.shape_circle_stroke_dddddd);
            ll_zhifubao.setBackgroundResource(R.drawable.shape_circle_stroke_ff8b00);
        }
    }

    private void order_payinfo() {
        CommLoading.showLoading(mContext);
        API_Manager.order_payinfo(mContext, orderId, payment, new OkHttpCallBack<BaseResponce<PayInfo_AliPayData>>() {
            @Override
            public void onSuccess(BaseResponce<PayInfo_AliPayData> baseResponce) {
                CommLoading.dismissLoading();
                alipay(baseResponce.getData().orderInfo);
            }

            @Override
            public void onFailure(BaseResponce<PayInfo_AliPayData> baseResponce) {
                CommLoading.dismissLoading();
                CommToast.showToast(mContext, baseResponce.message);
            }
        });
    }

    private void alipay(String orderInfo) {
        AliPay aliPay = new AliPay();
        //构造支付宝订单实体。一般都是由服务端直接返回。
        AlipayInfoImpli alipayInfoImpli = new AlipayInfoImpli();
        alipayInfoImpli.setOrderInfo(orderInfo);
        //策略场景类调起支付方法开始支付，以及接收回调。
        EasyPay.pay(aliPay, this, alipayInfoImpli, new IPayCallback() {
            @Override
            public void success() {
                CommToast.showToast(mContext, "支付成功");
                toResult();
            }

            @Override
            public void failed(int code, String msg) {
                CommToast.showToast(mContext, "支付失败");
                toResult();
            }

            @Override
            public void cancel() {
                CommToast.showToast(mContext, "支付取消");
                toResult();
            }
        });
    }

    private void toResult() {
        Intent intent = new Intent(mContext, PayResultActivity.class);
        intent.putExtra("orderId", orderId);
        startActivity(intent);
        finish();
    }
}
